﻿using MiniFox.Platform.File.Models;
using MiniFox.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Platform.File.Services.Impls
{
    /// <summary>
    /// 
    /// </summary>
    public class DefaultFileService : FileService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        public DefaultFileService(string group) : base(group)
        {
        }

        private FileInfo GetFileInfo(FileData fileData)
        {
            string fullName = ConvertVirtualPathToPhysicalPath(fileData);
            FileInfo fileInfo = new FileInfo(fullName);
            return fileInfo;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileData"></param>
        protected override void OnLoad(FileData fileData)
        {
            FileInfo fileInfo = GetFileInfo(fileData);
            var bytes = Tools.ReadFileAsBytes(fileInfo);
            fileData.Data = bytes;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileData"></param>
        /// <returns></returns>
        protected override async Task OnLoadAsync(FileData fileData)
        {
            FileInfo fileInfo = GetFileInfo(fileData);
            var bytes = await Tools.ReadFileAsBytesAsync(fileInfo);
            fileData.Data = bytes;
        }
    }
}
